package com.cw.jeeyt.service.impl.security;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityLoginLogBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.security.SecurityLoginLogDao;
import com.cw.jeeyt.dao.model.security.SecurityLoginLogDo;
import com.cw.jeeyt.service.converter.security.SecurityLoginLogConverter;
import com.cw.jeeyt.service.intf.security.SecurityLoginLogService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 登录日志信息业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SecurityLoginLogServiceImpl implements SecurityLoginLogService {

    private static final Logger logger = LoggerFactory.getLogger(SecurityLoginLogServiceImpl.class);
    @Autowired
    private SecurityLoginLogDao securityLoginLogDao;

    /**
     * 分页查询登录日志信息
     *
     * @param securityLoginLogBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SecurityLoginLogBo> queryByPage(SecurityLoginLogBo securityLoginLogBo, Integer startNum,
        Integer pageSize) throws Exception {

        if (securityLoginLogBo == null) {
            logger.error("登录日志信息为空");
            return null;
        }
        PageInfo<SecurityLoginLogDo> pageInfo = securityLoginLogDao.queryByPage(
            SecurityLoginLogConverter.bo2Do(securityLoginLogBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SecurityLoginLogConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻登录日志信息
     *
     * @param id
     * @return
     */
    @Override
    public SecurityLoginLogBo getSecurityLoginLogById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return SecurityLoginLogConverter.do2Bo(securityLoginLogDao.getById(id));
    }

    /**
     * 新增登录日志信息
     *
     * @param securityLoginLogBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SecurityLoginLogBo securityLoginLogBo) throws Exception {

        if (securityLoginLogBo == null) {
            logger.error("登录日志信息为空");
            return null;
        }
        securityLoginLogBo.setId(SequenceTool.nextId());
        securityLoginLogBo.setCreateDate(new Date());
        securityLoginLogBo.setUpdateDate(new Date());
        return securityLoginLogDao.insert(SecurityLoginLogConverter.bo2Do(securityLoginLogBo));
    }

    /**
     * 更新登录日志信息
     *
     * @param securityLoginLogBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SecurityLoginLogBo securityLoginLogBo) throws Exception {

        if (securityLoginLogBo == null) {
            logger.error("登录日志信息为空");
            return;
        }
        securityLoginLogBo.setUpdateBy(SessionUtil.getSessionUserId());
        securityLoginLogBo.setUpdateDate(new Date());
        securityLoginLogDao.update(SecurityLoginLogConverter.bo2Do(securityLoginLogBo));
    }

    /**
     * 删除登录日志信息
     *
     * @param securityLoginLogBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SecurityLoginLogBo securityLoginLogBo) throws Exception {

        if (securityLoginLogBo == null) {
            logger.error("登录日志信息为空");
            return;
        }
        securityLoginLogDao.delete(SecurityLoginLogConverter.bo2Do(securityLoginLogBo));
    }
}
